import React from 'react'
import { ActivityIndicator, Result, Icon, Tools, Toast } from 'weaver-mobile';

const getLabel = Tools.getLabel;

// 协作 公共工具类
const VotingConstant = {
	
	// api请求错误统一处理方法
  catchApiError: (result) => {
    return new Promise((resolve, reject) => {
      if (!result.api_status) {
        Toast.fail(result.msg || getLabel(30651, '操作失败'));
        reject(result);
        return;
      }
      resolve(result);
    });
  },
/* 统一增加参数 */
  PARAMS: {
    // _ec_device: 'mobile_ec',
    _ec_ismobile: 'true',
  }
}

export default VotingConstant;

/* Loading 页面 */
export const renderLoading = (
  <div className='voting-loading'>
    <div className='voting-loading-content'>
      <ActivityIndicator />
      <div style={{ marginTop: 8 }}>loading...</div>
    </div>
  </div>
);

/* 暂无数据 页面 */
export const renderNoData = () => {
  return (
    <div className='voting-noData'>
      <Result
        img={<Icon type="no-data" />}
        message={getLabel(83553, '暂无数据')}
      />
    </div>
  )
}

/* 深度比较两对象是否相等 */
export const compareParams = (propA = {}, propB = {}) => {
  let flag = true;
  // 如果需要把A对象拆分，需要该方法 -- 重塑A对象
  for (let a in propA) {
    if (a.includes('__')) {
      let splitItem = a.split("__");
      if (splitItem.length > 0) {
        splitItem.map(item => {
          if (!propA[item]) propA[item] = propA[a]
        })
      }
    }
  }

  for (let b in propB) {
    if (propA[b] || propA[b] === '' || propA[b] === 0) {
      if (propB[b] !== propA[b]) {
        flag = false;
        break
      }
    }
  }

  return flag;
}